package t_01_bio;

import lombok.Cleanup;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class BIOClient {
    static ExecutorService threadPool = Executors.newFixedThreadPool(10);

    public static void main(String[] args) throws IOException {
        for (int i = 0; i < 99; i++) {
            threadPool.submit(() -> {
                try {
                    Socket socket = new Socket("127.0.0.1", 6666);

                    @Cleanup InputStream inputStream = socket.getInputStream();
                    @Cleanup OutputStream outputStream = socket.getOutputStream();

                    outputStream.write("HelloServer，中国".getBytes());
                    outputStream.flush();
                    System.out.println("客户端消息已发送完毕！");

                    byte[] bytes = new byte[1024];
                    int len = inputStream.read(bytes);
                    System.out.println("服务器端返回消息：" + new String(bytes, 0, len));

                    // outputStream.close();
                    // inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            });
        }
        threadPool.shutdown();
    }
}
